package com.example.shortmessage.adapter;


import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.shortmessage.R;
import com.example.shortmessage.data.entity.MessageData;
import com.example.shortmessage.interfaces.DialogOnClickInterface;
import com.example.shortmessage.utils.DateUtils;

import java.util.List;

public class MyContentAdapter extends RecyclerView.Adapter<MyContentAdapter.ViewHolder> implements DialogOnClickInterface {
    private List<MessageData> messageDataLists;
    private Activity context;

    public MyContentAdapter(List<MessageData> messageDataLists, Activity context) {
        this.messageDataLists = messageDataLists;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewHolder viewHolder = new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_content, parent, false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        MessageData messageData = messageDataLists.get(position);
        if(messageData.getType()==1) {
            holder.tvContent2.setVisibility(View.GONE);
            holder.tvDate2.setVisibility(View.GONE);
            holder.tvDate1.setText(DateUtils.dateToString(messageData.getDate(), "yyyy年MM月dd日HH时mm分"));
            holder.tvContent1.setText(messageData.getBody());
        }else {
            holder.tvContent1.setVisibility(View.GONE);
            holder.tvDate1.setVisibility(View.GONE);
            holder.tvDate2.setText(DateUtils.dateToString(messageData.getDate(),"yyyy年MM月dd日HH时mm分"));
            holder.tvContent2.setText(messageData.getBody());
        }
    }

    @Override
    public int getItemCount() {
        return messageDataLists==null ? 0 : messageDataLists.size();
    }

    @Override
    public void interfacePositive() {
    }

    @Override
    public void interfaceNegative() {

    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvDate1;
        TextView tvDate2;
        TextView tvContent1;
        TextView tvContent2;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvDate1 = itemView.findViewById(R.id.tv_date_1);
            tvDate2 = itemView.findViewById(R.id.tv_date_2);
            tvContent1 = itemView.findViewById(R.id.tv_content_1);
            tvContent2 = itemView.findViewById(R.id.tv_content_2);
        }

    }
}
